package edu.sctu.jsoup.service;


import edu.sctu.jsoup.entity.Table3Entity;
import edu.sctu.jsoup.entity.TableFourEntity;
import edu.sctu.jsoup.repo.Table3Repo;
import edu.sctu.jsoup.repo.TableFourRepo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class TableFourService {

    @Autowired
    private Table3Repo table3Repo;

    @Autowired
    private TableFourRepo tableFourRepo;

    public void run() throws IOException {


        /*获取断点*/
        /*
        * 注意：
        * getTable4.txt文件同样需要自己在D盘dev下新建，并把初始值1写入
        * Table3Repo添加如下语句：
        * List<Table3Entity> findById(long nextStart);
        * */
        File filename = new File("D:\\dev\\getTable4.txt");

        InputStreamReader reader = new InputStreamReader(
                new FileInputStream(filename));

        BufferedReader br = new BufferedReader(reader);

        String line = br.readLine();

        long nextStart = (long)Integer.parseInt(line);//将读取到的字符串转化为int类型



        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // new Date()为获取当前系统时间
        String date = df.format(new Date());
        System.out.println("开始解析\n时间："+date);


        /*遍历表三并解析每个记录里面的网页源码*/
        for (long i = nextStart; i <=table3Repo.count(); i++) {//表三的总长为：table3EntityList.size()

            List<Table3Entity> next = table3Repo.findById(i);

            Document document = Jsoup.parse(next.get(0).getHtmlContent());

            Elements elements = document.select("div#subcontent").select("dl.list_dl");

            /*遍历所有的dl标签*/  /*第二个dl开始才是帖子*/
            for (int j = 1; j < elements.size(); j++) {
                TableFourEntity tableFourEntity = new TableFourEntity();

                /*设置相关信息并保存*/
                tableFourEntity.setWhereFrom(
                        next.get(0).getTitle()
                                +
                                next.get(0).getPage()
                );

                tableFourEntity.setTitle(
                        elements.get(j).select("dt").first()
                                .select("a").first().text()
                );

                tableFourEntity.setUrl(
                        "http://club.autohome.com.cn" +
                                elements.get(j).select("dt").first()
                                        .select("a").first().attr("href")
                );
                tableFourRepo.save(tableFourEntity);

            }


              /*存储下次请求的开始值或断点*/
            File writename = new File("D:\\dev\\getTable4.txt");

            writename.createNewFile(); // 创建新文件

            BufferedWriter out = new BufferedWriter(new FileWriter(writename));

            out.write(Integer.toString((int) (i + 1)));

            // 把缓存区内容压入文件
            out.flush();
            // 最后关闭文件
            out.close();
            System.out.println("已解析到第"+i+"条记录!" +
                    "\n下次解析的开始值"+(i+1)+"已记录！");


        }

        String endDate = df.format(new Date());//得到程序结束时间
        System.out.println("解析结束！！");
        System.out.println("开始时间："+date);
        System.out.println("结束时间："+endDate);

    }
}
